package com.xxxx.ifms.controller;

import com.xxxx.ifms.annoation.RequiredPermission;
import com.xxxx.ifms.base.BaseController;
import com.xxxx.ifms.base.ResultInfo;
import com.xxxx.ifms.query.VoucherQuery;
import com.xxxx.ifms.service.VoucherService;
import com.xxxx.ifms.utils.CookieUtil;
import com.xxxx.ifms.vo.Voucher;
import com.xxxx.ifms.vo.Voucher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("voucher")
public class VoucherController extends BaseController {
    @Resource
    private VoucherService voucherService;

    /**
     * 凭据信息数据查询（分页多条件查询）801001
     * @param voucherQuery
     * @return
     */
    @RequiredPermission(code = "801001")
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> queryVoucherByParams(VoucherQuery voucherQuery) {
        return voucherService.queryVoucherByParams(voucherQuery);
    }


    /**
     * 进入凭据信息管理页面  8010
     *
     * @param
     * @return java.lang.String
     */
    @RequiredPermission(code = "8010")
    @RequestMapping("index")
    public String index() {
        return "voucher/voucher";
    }


    /**
     * 更新凭据信息 801004
     *
     * @param voucher
     * @return com.xxxx.ifms.base.ResultInfo
     */
    @RequiredPermission(code = "801004")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateVoucher(Voucher voucher) {
        // 调用Service层的添加方法
        voucherService.updateVoucher(voucher);
        return success("凭据信息数据更新成功！");
    }


    /**
     * 添加凭据信息 801002
     *
     * @param voucher
     * @param request
     * @return
     */
    @RequiredPermission(code = "801002")
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addVoucher(Voucher voucher, HttpServletRequest request) {
        // 从Cookie中获取当前登录的用户名
        String userName = CookieUtil.getCookieValue(request, "userName");
        // 设置用户名到创建人
        voucher.setCreateMan(userName);
        // 调用Service层的添加方法
        voucherService.addVoucher(voucher);
        return success("凭据信息数据添加成功！");
    }


    @RequiredPermission(code = "801003")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteVoucher(Integer[] ids) {
        // 调用Service层的删除方法
        voucherService.deleteBatch(ids);
        return success("凭据信息数据删除成功！");
    }


    /**
     * 进入添加/修改凭据信息页面
     *
     * @param
     * @return java.lang.String
     */
    @RequiredPermission(code = "801004")
    @RequestMapping("toVoucherPage")
    public String toVoucherPage(Integer voucherId, HttpServletRequest request) {

        // 判断voucherId是否为空
        if (voucherId != null) {
            // 通过ID查询营销机会数据
            Voucher voucher = voucherService.selectByPrimaryKey(voucherId);
            // 将数据设置到请求域中
            request.setAttribute("voucher", voucher);
        }

        return "voucher/add_update";
    }

}
